from pyglet import gl
import pymt as mt
import curve


class Drawer(object):

    def drawPatchCurve(self, points):
        """
            Drawer a curve that contain the points of the parameter point_list.
            
            The color of curve is determined by combination of colors 
            represented by parameters r (red), g (green) and b (blue).
        """
        gl.glColor3f(1.0, 0.0, 0.0)
        gl.glBegin(gl.GL_LINE_STRIP)
        for i in range(4):
            gl.glVertex3f(points[i][0], points[i][1], points[i][2])
        gl.glEnd()
        


class TouchTracer(mt.MTWidget):
    def __init__(self):
        super(TouchTracer, self).__init__()
        self.touchPositions = {}
        self.cp_list = []
        self.draw_curve = False

    def on_touch_down(self, touches, touchID, x,y):
        self.touchPositions[touchID] = [(touchID,x,y)]
        self.draw_curve = False


    def on_touch_up(self, touches, touchID,x,y):
        if len(self.cp_list)>4:
            self.draw_curve = True
        #self.show()
        
    def show(self):
        if self.draw_curve:
            factory = curve.BSplineFactory()
            curva = factory.createBSplineCurve("CURVE", self.cp_list, "Global" )
            quantity_patch = len(curva.cp_list) - 3
            print "%s  -  %s  -  %s"%(len(self.cp_list), len(curva.cp_list), quantity_patch)
            drawer = Drawer()
            curva.definePatch(0)
            for i in range(quantity_patch):
                print i
                curva.showPatch()
                print curva.cp_list
                points = curva.generatePatch(4)
                drawer.drawPatchCurve(points)
                curva.nextPatch()


    def on_touch_move(self, touches, touchID, x, y):
        self.touchPositions[touchID].append((x,y))
        print "id: %s\t%s\t%s\t0.0"%(touchID,x,y)
        self.cp_list.append([x,y,0.0])
        #print self.cp_list

    def draw(self):
        gl.glClearColor(0.4,0.4,0.4,1.0)
        w.clear()
        for point in self.cp_list:
                gl.glPointSize(3)
                gl.glColor3f(0.0, 0.0, 1.0)
                gl.glBegin(gl.GL_POINTS)
                gl.glVertex2f(point[0], point[1])
                gl.glEnd()
        if self.draw_curve:
            factory = curve.BSplineFactory()
            curva = factory.createBSplineCurve("CURVE", self.cp_list, "Global" )
            quantity_patch = len(curva.cp_list) - 3
            #print "%s  -  %s  -  %s"%(len(self.cp_list), len(curva.cp_list), quantity_patch)
            drawer = Drawer()
            curva.definePatch(0)
            for i in range(quantity_patch):
                #print i
                #curva.showPatch()
                #print curva.cp_list
                points = curva.generatePatch(4)
                drawer.drawPatchCurve(points)
                curva.nextPatch()
            #del(curva)
            gl.glFlush()
            
    def on_object_down(self, touches, touchID, x,y):
        print touches
        print touchID
        print (x, y)
    
if __name__ == '__main__':
    w = mt.MTWindow(fullscreen=False)
    w.add_widget(TouchTracer())
    mt.runTouchApp()
